<template>
  <div class="app-container">
    <el-form :inline="true" :model="formData" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="formData.roleName" placeholder="角色名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="formData.roleCode" placeholder="角色代码" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-if="checkPermission(['admin:role:list'])" @click="getDataList">查询</el-button>
        <el-button v-if="checkPermission(['admin:role:saveOrUpdate'])" type="primary" @click="addData">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table
      :data="list"
      border
      style="width: 100%" v-loading="listLoading">
      <el-table-column align="center" type="index" width="50" />
      <el-table-column align="center" prop="roleCode" label="角色代码" />
      <el-table-column align="center" prop="roleName" label="角色名称" />
      <el-table-column align="center" prop="roleDesc" label="角色描述" />
      <el-table-column align="center" prop="createTime" label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.createTime | gmtCreateFilter }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        width="100"
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="checkPermission(['admin:role:info'])" type="text" size="small" @click="editHandle(scope.row.id)">编辑</el-button>
          <el-button v-if="checkPermission(['admin:role:remove'])" type="text" size="small" @click="removeHandle(scope.row.id)">删除</el-button>
          <el-button v-if="checkPermission(['admin:role:getRoleAll'])" type="text" size="small" @click="persHandle(scope.row.id)">授权</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页插件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[20, 50, 100, 200]"
      :page-size="formData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <role-add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></role-add-or-update>
    <pers-add-or-update v-if="persVisible" ref="pers" @refreshDataList="getDataList"></pers-add-or-update>
  </div>
</template>

<script>
  import adminRoleApi from '@/api/core/sys/admin-role'
  import RoleAddOrUpdate from './role-add-or-update.vue'
  import PersAddOrUpdate from './pers-add-or-update.vue'
  export default {
    filters: {
      gmtCreateFilter (code) {
        const date = new Date(code)
        const y = date.getFullYear()
        let MM = date.getMonth() + 1
        MM = MM < 10 ? ('0' + MM) : MM
        let d = date.getDate()
        d = d < 10 ? ('0' + d) : d
        let h = date.getHours()
        h = h < 10 ? ('0' + h) : h
        let m = date.getMinutes()
        m = m < 10 ? ('0' + m) : m
        let s = date.getSeconds()
        s = s < 10 ? ('0' + s) : s
        return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
      }
    },
    data () {
      return {
        formData: {
          pageNo: 1,
          pageSize: 20,
          roleName: '',
          roleCode: ''
        },
        list: [],
        total: 0,
        addOrUpdateVisible: false,
        listLoading: false,
        persVisible: false
      }
    },
    components: {
      RoleAddOrUpdate,
      PersAddOrUpdate
    },
    created () {
      this.getDataList()
    },
    methods: {
      // 查询列表
      getDataList() {
        this.listLoading = true
        adminRoleApi.listAll(this.formData).then(res => {
          this.list = res.data.pages.records
          this.total = res.data.pages.total
          this.listLoading = false
        }).catch(res => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      },
      // 每页多少条数据
      handleSizeChange(val) {
        this.pageSize = val
      },
      // 当前第几页
      handleCurrentChange(val) {
        this.pageNo = val
      },
      // 新增
      addData() {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init()
        })
      },
      // 编辑
      editHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      removeHandle(id) {
        this.$confirm(`确定对[id=${id}]进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          adminRoleApi.removeById(id).then(res => {
            if (res && res.code === 0) {
              this.$message({
                message: '删除成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(res.message)
            }
          }).catch()
        }).catch(() => {
          this.$message('取消')
        })
      },
      // 授权
      persHandle(id) {
        this.persVisible = true
        this.$nextTick(() => {
          this.$refs.pers.init(id)
        })
      }
    }
  }
</script>

<style scoped></style>